﻿using System.Collections.Generic;
using System.Linq;
using FellowshipOne.API;
using FellowshipOne.API.Base;
using FellowshipOne.API.Base.Interfaces;
using FellowshipOne.API.Groups.Model;
using FellowshipOne.API.Groups.QueryObject;
using FellowshipOne.API.Realms;
using MasVida.WebApi.Controllers;
using MasVida.WebApi.Models.Requests;
using MasVida.WebApi.Models.Responses;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json.Linq;
using Shouldly;

namespace MasVida.WebApi.Tests.Controllers
{
    [TestClass]
    public class GroupControllerTests
    {
        [TestMethod]
        public void GetGroupsForUser()
        {
            // Variables
            const int persondId = 54;
            Mock<IApiSet<Member>> memberMock = new Mock<IApiSet<Member>>();
            memberMock.Setup(m => m.Search<MemberCollection>(It.Is<MemberQO>(q => q.PersonId == persondId)))
                .Returns(new MemberCollection(new List<Member>()
                {
                    new Member() { Group = new Group() {ID = 987}},
                    new Member() { Group = new Group() {ID = 97}}
                }));
            Mock<IGroupsRealm> groupsMock = new Mock<IGroupsRealm>();
            groupsMock.Setup(g => g.Member).Returns(memberMock.Object);
            IRestClient restClient = Mock.Of<IRestClient>();
            restClient.GroupsRealm = groupsMock.Object;
            GroupRequest groupRequest = new GroupRequest() {PersonId =  persondId, AccessTicket = new F1OAuthTicket()};
            JObject jObject = JObject.FromObject(groupRequest);

            // Construct controller
            GroupController controller = new GroupController(restClient);

            // Method call
            GroupsResponse groupsResponse = controller.GetGroupsForUser(jObject);

            memberMock.Verify(m => m.Search<MemberCollection>(It.IsAny<MemberQO>()), "Search was not performed");
            groupsResponse.ShouldNotBe(null);
            groupsResponse.Success.ShouldBe(true, "Method should be successfull");
            groupsResponse.Groups.ToList().Count.ShouldBeGreaterThan(1, "There should be at least 2 groups retrieved");
        }


        
    }
}
